using System;
using System.Collections;
using PickGold.Charting;

namespace PickGold.Charting.ChartTypes
{
	internal class HundredPercentStackedBarChart : StackedBarChart
	{
		private Hashtable _stackedGroupsTotalPerPoint;

		public HundredPercentStackedBarChart()
		{
			base.hundredPercentStacked = true;
		}

		public override double GetYValue(CommonElements common, ChartArea area, Series series, DataPoint point, int pointIndex, int yValueIndex)
		{
			double[] numArray = null;
			string seriesStackGroupName = StackedColumnChart.GetSeriesStackGroupName(series);
			if (this._stackedGroupsTotalPerPoint == null)
			{
				this._stackedGroupsTotalPerPoint = new Hashtable();
				foreach (string str2 in base.stackGroupNames)
				{
					Series[] seriesArray = StackedColumnChart.GetSeriesByStackedGroupName(common, str2, series.ChartTypeName, series.ChartArea);
					common.DataManipulator.CheckXValuesAlignment(seriesArray);
					double[] numArray2 = new double[series.Points.Count];
					for (int i = 0; i < series.Points.Count; i++)
					{
						numArray2[i] = 0.0;
						foreach (Series series2 in seriesArray)
						{
							numArray2[i] += Math.Abs(series2.Points[i].YValues[0]);
						}
					}
					this._stackedGroupsTotalPerPoint.Add(str2, numArray2);
				}
			}
			numArray = (double[])this._stackedGroupsTotalPerPoint[seriesStackGroupName];
			if (!area.Area3DStyle.Enable3D && ((point.YValues[0] == 0.0) || point.IsEmpty))
			{
				return 0.0;
			}
			if (!area.Area3DStyle.Enable3D || (yValueIndex == -2))
			{
				if (numArray[pointIndex] == 0.0)
				{
					return 0.0;
				}
				return ((point.YValues[0] / numArray[pointIndex]) * 100.0);
			}
			double naN = double.NaN;
			if (yValueIndex == -1)
			{
				double crossing = area.GetAxis(AxisName.Y, series.YAxisType, series.YSubAxisName).Crossing;
				naN = this.GetYValue(common, area, series, point, pointIndex, 0);
				if (naN >= 0.0)
				{
					if (!double.IsNaN(base.prevPosY))
					{
						crossing = base.prevPosY;
					}
				}
				else if (!double.IsNaN(base.prevNegY))
				{
					crossing = base.prevNegY;
				}
				return (naN - crossing);
			}
			base.prevPosY = double.NaN;
			base.prevNegY = double.NaN;
			foreach (Series series3 in common.DataManager.Series)
			{
				if (((string.Compare(series.ChartArea, series3.ChartArea, StringComparison.Ordinal) != 0) || (string.Compare(series.ChartTypeName, series3.ChartTypeName, StringComparison.OrdinalIgnoreCase) != 0)) || (!series3.IsVisible() || (seriesStackGroupName != StackedColumnChart.GetSeriesStackGroupName(series3))))
				{
					continue;
				}
				if (double.IsNaN(naN))
				{
					if (numArray[pointIndex] == 0.0)
					{
						naN = 0.0;
					}
					else
					{
						naN = (series3.Points[pointIndex].YValues[0] / numArray[pointIndex]) * 100.0;
					}
				}
				else
				{
					if (numArray[pointIndex] == 0.0)
					{
						naN = 0.0;
					}
					else
					{
						naN = (series3.Points[pointIndex].YValues[0] / numArray[pointIndex]) * 100.0;
					}
					if ((naN >= 0.0) && !double.IsNaN(base.prevPosY))
					{
						naN += base.prevPosY;
					}
					if ((naN < 0.0) && !double.IsNaN(base.prevNegY))
					{
						naN += base.prevNegY;
					}
				}
				if (string.Compare(series.Name, series3.Name, StringComparison.Ordinal) == 0)
				{
					break;
				}
				if (naN >= 0.0)
				{
					base.prevPosY = naN;
				}
				else
				{
					base.prevNegY = naN;
				}
			}
			if (naN <= 100.0)
			{
				return naN;
			}
			return 100.0;
		}

		public override void Paint(ChartGraphics graph, CommonElements common, ChartArea area, Series seriesToDraw)
		{
			this._stackedGroupsTotalPerPoint = null;
			base.Paint(graph, common, area, seriesToDraw);
		}

		public override bool HundredPercent
		{
			get
			{
				return true;
			}
		}

		public override bool HundredPercentSupportNegative
		{
			get
			{
				return true;
			}
		}

		public override string Name
		{
			get
			{
				return "100%StackedBar";
			}
		}
	}
}

